package com.shundr.common.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 一个可以自动换行的view group
 * @author zhaoyy
 *
 */
public class AutoWrapLineLayout extends ViewGroup {
	
	private int VIEW_MARGIN = 2; 

	public AutoWrapLineLayout(Context context) {
		super(context);
		VIEW_MARGIN = dp2px(VIEW_MARGIN);
	}

	public AutoWrapLineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        VIEW_MARGIN = dp2px(VIEW_MARGIN);
    }

    @SuppressLint("NewApi")
	public AutoWrapLineLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        VIEW_MARGIN = dp2px(VIEW_MARGIN);
    }
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		
		final int count = getChildCount(); 
		   
        int row = 0;
   
        int lengthX = l; 
   
        int lengthY = t; 
   
        for (int i = 0; i < count; i++) { 
   
            final View child = this.getChildAt(i); 
   
            int width = child.getMeasuredWidth(); 
   
            int height = child.getMeasuredHeight(); 
   
            lengthX += width + VIEW_MARGIN; 
   
            lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height 
                    + t; 
   
            if (lengthX > r) { 
   
                lengthX = width + VIEW_MARGIN + l; 
   
                row++; 
   
                lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height 
                        + t; 
   
            } 
   
            child.layout(lengthX - width, lengthY - height, lengthX, lengthY); 
   
        } 
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = getMeasuredWidth();
		
		int w = 0;
		int h = 0;
		
		for (int index = 0; index < getChildCount(); index++) { 
			   
            final View child = getChildAt(index); 
   
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 
            
            int childH = child.getMeasuredHeight() + VIEW_MARGIN * 2;
   
            int childW = child.getMeasuredWidth() + VIEW_MARGIN;
            
            w += childW;
            
            if (h == 0) {
            	h = childH;
            }
            if (w > width) {
            	h += childH;
            	w = childW;
            }
            
        } 
   
		if (h == 0) {// have no child
			h = dp2px(24);
		}
		
        setMeasuredDimension(resolveSize(widthMeasureSpec, widthMeasureSpec),  
                resolveSize(h + dp2px(4) * 2, heightMeasureSpec));  
	}
	
	private int dp2px(int dp) {
		float density = getContext().getResources().getDisplayMetrics().density;
		return (int) (dp * density + 0.5f);
	}
	
	@Override
	public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
	{
		return new MarginLayoutParams(getContext(), attrs);
	}

	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(
			ViewGroup.LayoutParams p) {
		return new MarginLayoutParams(p);
	}
	
}